package com.ygg.bootservice.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ygg.bootcommon.config.NotIntercept;
import com.ygg.bootcommon.model.bo.ReturnBO;
import com.ygg.bootcommon.model.entity.Token;
import com.ygg.bootservice.service.ITokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
/**
 * @author ：longyang
 * @date ：Created in 2020/12/15 9:37
 * @description：令牌控制层
 */
@RestController
@RequestMapping("api/token")
public class TokenController {
    @Autowired
    private ITokenService iTokenService;

    /**
     * 申请令牌
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getToken", method = {RequestMethod.POST})
    public String getToken(@RequestBody Map<String,String> map) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            String token = iTokenService.getToken(map);
            ReturnBO bo = new ReturnBO();
            bo.setState(0);
            bo.setObject(token);
            return gson.toJson(bo);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("令牌申请失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }

    /**
     * 查询所有未删除的令牌
     * @return
     * @throws Exception
     */
    @NotIntercept
    @RequestMapping(value = "/getAllToken", method = {RequestMethod.POST})
    public String getAllToken(@RequestBody Map<String,String> map) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            List<Token> tokenList = iTokenService.getAllToken(map);
            ReturnBO result = new ReturnBO();
            result.setState(0);
            result.setObject(tokenList);
            return gson.toJson(result);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO result = new ReturnBO();
            result.setState(1);
            result.setObject("查询失败："+e.getMessage());
            return gson.toJson(result);
        }
    }

    /**
     * 更新令牌
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateToken", method = {RequestMethod.POST})
    public String updateToken(@RequestBody Map<String,String> map) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            iTokenService.updateToken(map);
            ReturnBO bo = new ReturnBO();
            bo.setState(0);
            bo.setObject("更新成功");
            return gson.toJson(bo);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("更新失败：" + e.getMessage());
            return gson.toJson(bo);
        }
    }


}
